package com.magic.springboot.controller;


import com.magic.springboot.pojo.Books;
import com.magic.springboot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BooksController {
    @Autowired
    private BookService bookService;

    @RequestMapping("/allBook")
    private String list(Model model) {
        List<Books> books = bookService.queryAllBook();
        model.addAttribute("books", books);
        return "allBook";
    }

    @RequestMapping("/toAddBook")
    private String toAddPaper() {
        return "addBook";
    }

    @RequestMapping("/addBook")
    private String addPaper(Books books) {
        bookService.addBook(books);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/toUpdateBook")
    private String toUpdateBook(Model model, Integer bookID) {
        System.out.println(bookID);
        Books books = bookService.queryBookById(bookID);
        System.out.println(books);
        model.addAttribute("book", books);
        return "updateBook";
    }

    @RequestMapping("/updateBook")
    private String updateBook(Model model, Books book) {
        System.out.println(book);
        bookService.updateBook(book);
        Books books = bookService.queryBookById(book.getBookID());
        model.addAttribute("books", books);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/deleteBook")
    private String deleteBook(Integer bookID) {
        bookService.deleteBookById(bookID);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/queryLikeBook")
    private String queryLikeBook(String queryBookName, String queryCategory, Model model) {
        List<Books> books = bookService.queryLikeBook(queryBookName, queryCategory);
        model.addAttribute("books", books);
        return "allBook";
    }

}
